package com.banciyuan.bcywebview.utils.string;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.URLSpan;
import android.widget.TextView;

/**
 * Created by squall on 2015/10/10.
 */
public class UnderLineUtil {

    public static Spannable DisUnderLine(Context mContext, SpannableString s) {
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span : spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            s.removeSpan(span);
            span = new URLSpanNoUnderline(span.getURL());
            ((URLSpanNoUnderline) span).setContext(mContext);
            s.setSpan(span, start, end, 0);
        }
        return s;
    }

    public static void DisUnderLine(Context mContext, TextView textView) {
        if (textView.getText() instanceof Spannable) {
            Spannable s = (Spannable) textView.getText();
            URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
            for (URLSpan span : spans) {
                int start = s.getSpanStart(span);
                int end = s.getSpanEnd(span);
                s.removeSpan(span);
                span = new URLSpanNoUnderline(span.getURL());
                ((URLSpanNoUnderline) span).setContext(mContext);
                s.setSpan(span, start, end, 0);
            }
            textView.setText(s);
        }
    }

    public static void DisUnderLine(Context mContext, TextView textView, String url) {
        if (textView.getText() instanceof Spannable) {
            Spannable s = (Spannable) textView.getText();
            URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
            for (URLSpan span : spans) {
                int start = s.getSpanStart(span);
                int end = s.getSpanEnd(span);
                s.removeSpan(span);
                span = new URLSpanNoUnderline(url);
                ((URLSpanNoUnderline) span).setContext(mContext);
                s.setSpan(span, start, end, 0);
            }
            textView.setText(s);
        }
    }
}
